import { query, create, get, update, remove } from '../../../framework/service/index';
import { ToastTips } from '../../../common/toastTips/tosat';

export default {

  namespace: 'message',

  state: {
    list: [],
    noticeInfo:[],
    newProblemResponse:[],
    newPartTimeMessage:[]
  },

  subscriptions: {
    setup({ dispatch ,history }) {
      history.listen(location => {
        if(location.pathname === '/message') {

          // 获取咨询信息
          dispatch({
            type: 'getProblemResponseInfo',
          })

          // 获取消息信息
          dispatch({
            type: 'getPartTimeMessageInfo',
          })

          //获取公告信息
          dispatch({
            type: 'getNoticeInfo',
          })

          dispatch({
            type: 'allPartTime/querySuccess',
            payload:{
              isShow:-1,
              ordeyIndex:0,
              areaIndex:0,
              typeNameIndex:0,
              key:'',
              typeId:'',
              district :'',
            }
          })
        }
      })
    }
  },

  effects: {

    //获取咨询信息
    *getProblemResponseInfo({ payload }, { call, put }) {
      const {code, data, message } = yield call(query, '/api/job/job-consult/me/unreaded-count', {});
      // console.log('获取咨询信息 code', JSON.stringify(code));
      // console.log('获取咨询信息 data', JSON.stringify(data));
      // console.log('获取咨询信息 message', JSON.stringify(message));
      if (code === 200 && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            newProblemResponse: data
          }
        });
      }else{
        console.log('获取咨询信息失败');
      }
    },

    //获取消息信息
    *getPartTimeMessageInfo({ payload }, { call, put }) {
      const {code, data, message } = yield call(query, '/api/job/message-history/me/unreaded-count', {});
      // console.log('获取消息信息 code', JSON.stringify(code));
      // console.log('获取消息信息 data', JSON.stringify(data));
      // console.log('获取消息信息 message', JSON.stringify(message));
      if (code === 200 && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            newPartTimeMessage: data
          }
        });
      }else{
        console.log('获取消息信息失败');
      }
    },

    //获取公告信息
    *getNoticeInfo({ payload }, { call, put }) {

      const {code, data, message } = yield call(query, '/api/term/config/bytype/CONSULTATION', {});
      // console.log('code', JSON.stringify(code));
      // console.log('data', JSON.stringify(data));
      // console.log('message', JSON.stringify(message));
      if (code === 200 && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            noticeInfo: data
          }
        });
      }else{
        console.log('获取公告信息失败');
      }
    },

  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload }
    }
  }
}
